import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, getprofile, getimg } from '@/api/user'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    // 存储token
    updataToken(state, token) {
      state.token = token
      // 存储到cookile
      setToken(token)
    },
    // 删除token
    clearToken(state) {
      state.token = ''
      removeToken()
    },
    // 存储用户信息
    loinfo(state, userInfo) {
      state.userInfo = userInfo
    },
    // 清空用户信息
    clearInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async userLogin(context, formdata) {
      console.log(context)
      const res = await login(formdata)
      // 存储到vuex中
      context.commit('updataToken', res.data)
    },
    async particulars(context, userInfo) {
      const res = await getprofile(userInfo)
      console.log(res, '用户信息')
      const re = await getimg(res.data.userId)
      console.log(re, '头像')
      // 存储到vuex
      context.commit('loinfo', { ...res.data, ...re.data })
    },
    logout(context) {
      context.commit('clearToken')
      context.commit('clearInfo')
    }

  }
}
